// pages/Riders/RidersLogin/RidersLogin.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { riderLogin } from '../../../api/login';
import { queryRiderInfo } from '../../../api/riders';
import { ACCESS_TOKEN } from '../../../utils/types';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    assetsAddress: assetsAddress,
    navBgColor: 'none',
    isPassword: true, // 是否是密码框
    phone: wx.getStorageSync('riderPhone') || '',
    password: wx.getStorageSync('riderPassword') || '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 返回上一页
  back(){
    wx.navigateBack({
      delta: 1,
    })
  },
  // 输入框变化
  onPhoneChange(e){
    this.setData({
      phone: e.detail,
    })
  },
  onPasswordChange(e){
    this.setData({
      password: e.detail,
    })
  },
  iptTypeChange(){
    console.log('123123')
    this.setData({
      isPassword: !this.data.isPassword
    })
  },
  // 登录
  login(){
    let _this = this;
    if(_this.data.phone == ''){
      wx.showToast({
        title: '请输入骑手账号',
        icon: 'none',
      })
      return;
    }
    if(_this.data.password == ''){
      wx.showToast({
        title: '请输入骑手密码',
        icon: 'none',
      })
      return;
    }
    riderLogin({account: _this.data.phone,password: _this.data.password})
    .then(res => {
      if(res.success){
        // wx.setStorageSync(ACCESS_TOKEN, res[ACCESS_TOKEN]);
        queryRiderInfo({phone: _this.data.phone})
        .then(res => {
          wx.setStorageSync('riderUserId', res.data.content[0].userId);
          wx.setStorageSync('riderPhone', res.data.content[0].phone);
          wx.setStorageSync('riderPassword', _this.data.password);
          wx.setStorageSync('riderName', res.data.content[0].name);
          wx.navigateTo({
            url: '/pages/Riders/RiderOrders/RiderOrders',
          })
        })
        
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
})